﻿using LD.BC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.Tool
{
    public partial class ToolIOMonitorByNotityFrm :FrmToolBase  
    {
        public ToolIOMonitorByNotityFrm()
        {
            InitializeComponent();
            this.Shown += ToolMoveFrm_Shown;
            this.btn_ok.Click += Btn_ok_Click;
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            if (this.cob_io.SelectedIndex >= 0)
            {
                IoMonitor ioMonitor = new IoMonitor()
                {
                    IoType = cob_DIorDO.SelectedIndex,
                    IoName = this.cob_io.Text,
                    Low_Hig = this.cob_lowHig.SelectedIndex,
                };
                this.TB_Tool.Param = Newtonsoft.Json.JsonConvert.SerializeObject(ioMonitor);
            }
        }

        private void ToolMoveFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }
        private void Init()
        {
            this.cob_DIorDO.SelectedIndex = -1;
            if (this.TB_Tool.Param == null)
                return;

            var tool = Newtonsoft.Json.JsonConvert.DeserializeObject<IoMonitor>(this.TB_Tool.Param);
            if (tool != null)
            {
                this.cob_DIorDO.SelectedIndex = tool.IoType;
                this.cob_io.Text = tool.IoName;
                this.cob_lowHig.SelectedIndex = tool.Low_Hig;
            }
        }

        private void cob_DIorDO_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cob_io.Items.Clear();
            var io = BcFactory.Instance.Config.IoList.Where(p => p.IoType == cob_DIorDO.SelectedIndex).ToList();
            foreach (var item in io)
            {
                this.cob_io.Items.Add(item.Name);
            }
        }
    }
}
